package tutorial.action;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ResponseUtil;
import org.seasar.struts.util.UploadUtil;

import tutorial.form.MultiUploadForm;

public class MultiUploadAction {

    @ActionForm
    @Resource
    protected MultiUploadForm multiUploadForm;

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected ServletContext application;

    private final static String INDEX = "index.jsp";

    @Execute(validator = false)
    public String index() {

        UploadUtil.checkSizeLimit(request);
        return INDEX;
    }

    @Execute(input = INDEX)
    public String upload() throws IOException {

        upload(multiUploadForm.Filedata);
        return null;
    }

    protected void upload(FormFile file) throws IOException {

        String path = application.getRealPath("/show/" + file.getFileName());

        UploadUtil.write(path, file);
        ResponseUtil.write("{'photo_url' : '../show/" + file.getFileName()
            + "'}");
    }
}